<!-- 注册
 1. vue始终在用双向绑定的思想来准备前后端交互，每一个vue都会定义好变量给 表单（form）或者表格（table）中的html元素的值绑定
-->
<template>
  <div><!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
    <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
    <el-dialog title="提交订单" :visible.sync="addDialogIsShow" :before-close="handleClose" :close-on-click-modal="false"
               append-to-body>
      <el-form :model="orderForm" label-width="80px" ref="addOrderForm">
        <el-form-item label="下单日期">
          <el-date-picker type="date" placeholder="开始日期" v-model="orderForm.uresdate"
                          style="width: 100%;"></el-date-picker>
          <el-date-picker type="date" placeholder="结束日期" v-model="orderForm.uresendDate"
                          style="width: 100%;"></el-date-picker>

        </el-form-item>
        <el-form-item label="定金" >
          <el-col :span="9">
            <el-input v-model="orderForm.uresprice"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="菜品选择">
          <el-col :span="9">
            <el-input v-model="orderForm.orderformitems[0].oiname"></el-input>
          </el-col>
          <el-col :span="9">
            <el-input v-model="orderForm.orderformitems[0].oidesc" placeholder="备注"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="场地说明">
          <el-col :span="9">
            <el-input v-model="orderForm.orderformitems[1].oiname"></el-input>
          </el-col>
          <el-col :span="9">
            <el-input v-model="orderForm.orderformitems[1].oidesc" placeholder="备注"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="附加说明">
          <el-col :span="9">
            <el-input v-model="orderForm.orderformitems[2].oiname"></el-input>
          </el-col>
          <el-col :span="9">
            <el-input v-model="orderForm.orderformitems[2].oidesc" placeholder="备注"></el-input>
          </el-col>
        </el-form-item>
      </el-form>

      <span>
      <el-button type="success" @click="submitForm('addOrderForm')">提交</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import rules from "../../../../static/js/validator/rules.js"
  import utils from "../../../../static/js/utils/index.js"
  import api from "../../../../axios/api";

  export default {
    name: "addOrder",
    data() {
      return {
        //order:{name:"",pic:"",date:""},
        uno: "",
        user: "",
        addDialogIsShow: true,
        rno: "",

        orderForm: {
          uresState: "未处理",
          uresprice:"200",
          "rno": this.rno,
          "uresDate": "1111",
          "uno": this.uno,
          "uresendDate": "",
          "orderformitemss": "",
          "orderformitems": [{"oiname": "", "oidesc": ""}, {"oiname": "", "oidesc": ""}, {"oiname": "", "oidesc": ""}]
        }  //  addOrderRules:{name:[{required:true,message:"球队名称不能为空",trigger:'blur'},
        //    {max:20,min:2,message:'长度异常 长度在2-20之间',trigger:'blur'},{pattern:/^[\u0391-\uFFE5A-Za-z]+$/,message:'球队名称必须为中文',trigger:'blur'}]}
      }
    },
    methods: {
      submitForm: function (addOrderForm) {
        this.orderForm.rno = this.rno;
        this.$refs[addOrderForm].validate(valid => {//此处refs对象和标签上的ref对应
          if (valid) {
            //如果通过验证 to do...
            console.log(this.orderForm);
            this.addOrder();
            this.handleClose();

          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      //页面初始化时加载的事件
      init(date, rno, uno, user) {
        this.uno = uno;
        this.user = user;
        console.log(uno);
        this.rno = rno;
        this.orderForm.rno = this.rno;
        // date = new Date(date)

        console.log("-----------addOrder");
        console.log(date);
        this.addDialogIsShow = true;
        // this.order.date = utils.FD.Fd().initCNDate(date);
      },

      //关闭页面
      handleClose() {
        this.addDialogIsShow = false;
        //给父组件传参
        this.$emit("addDialogIsShow", this.addDialogIsShow);
      },
      addOrder: function () {
        this.orderForm.orderformitemss = JSON.stringify(this.orderForm.orderformitems)
        this.orderForm.uno = this.uno;
        this.orderForm.user = this.user;
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
        api.setDataFromAxios("/api/orderFormController/addOrderForm/", this.orderForm, "post"
        ).then(dto => {
          console.log(dto);
          if (dto.code == 200) {
            this.$message.success("下单成功")
          } else {

            this.$message.error(dto.msg)
          }

        });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      }
    },
    //监听
    watch: {
      dialogisShow(newValue, oldValue) {
        this.addDialogIsShow = newValue;
      }
    }
  }
</script>

<style scoped>

</style>
